package com.zhangjianling.premp.controller.sys;

import com.zhangjianling.premp.controller.base.BaseController;
import com.zhangjianling.premp.entity.sys.ResultEntity;
import com.zhangjianling.premp.entity.sys.SysMenu;
import com.zhangjianling.premp.entity.sys.SysUser;
import com.zhangjianling.premp.server.sys.SysMenuService;
import com.zhangjianling.premp.utils.MenuUtil;
import com.zhangjianling.premp.utils.SystemAopLog;
import com.zhangjianling.premp.utils.TimeHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 系统菜单管理
 * 
 * @author zhangjl
 *
 */
@RestController
@RequestMapping("sysMenu")
public class SysMenuController extends BaseController {
	@Resource
	private SysMenuService sysMenuService;

	/**
	 * 分页菜单列表
	 * @return
	 */
	@RequiresPermissions("sysMenu:pageList")
	@PostMapping("pageList")
	public ResultEntity pageList(@RequestParam(value = "pageCurrent", defaultValue = "1") String pageCurrent, SysMenu menu) {
		ResultEntity res = new ResultEntity();
		try {
			Page<SysMenu> menuPage = sysMenuService.pageList(Integer.parseInt(pageCurrent), PAGESIZE, "createTime",menu);
			if (menuPage.getTotalPages() > 0) {
				res.setData(menuPage);
				res.setMessage(GET_SUCCESS);
				res.setCode(SUCCESS);
			} else {
				res.setMessage(IS_NULL);
				res.setCode(NULL);
			}
		} catch (Exception e) {
			res.setMessage(GET_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);

		}
		return res;
	}
	
	/**
	 * 菜单保存
	 * @param menu
	 * @return
	 */
	@PostMapping("saveMenu")
	@SystemAopLog(description="菜单保存")
	public ResultEntity saveMenu(SysMenu menu,String parentMenuPid){
		ResultEntity res = new ResultEntity();
		Subject subject = SecurityUtils.getSubject();
		SysUser currentUser = (SysUser)subject.getPrincipal();
		String curUserName =  currentUser.getUserName();
		try {
			if(StringUtils.isNotBlank(parentMenuPid)){
				SysMenu pmenu = new  SysMenu();
				pmenu.setMenuId(parentMenuPid);
				menu.setParentMenu(pmenu);
			}
			if(StringUtils.isNotBlank(menu.getMenuId())){
				menu.setUpdateTime(TimeHelper.getCurrentTime());
				menu.setUpdateUser(curUserName);
			}else{
				menu.setCreateTime(TimeHelper.getCurrentTime());
				menu.setCreateUser(curUserName);
			}
			sysMenuService.saveMenu(menu);
			res.setMessage(OPERATE_SUCCESS);
			res.setCode(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			res.setMessage(OPERATE_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}
	
	
	/**
	 * 删除菜单
	 * @return
	 */
	@PostMapping("delMenu")

	public ResultEntity delMenu(String ID){
		ResultEntity res = new ResultEntity();
		try {
			sysMenuService.deleteMenu(ID);
			res.setMessage(DELETE_SUCCESS);
			res.setCode(SUCCESS);
		} catch(Exception e){
			e.printStackTrace();
			res.setMessage(DELETE_ERROR+":"+e.getMessage());
			res.setCode(ERROR);

		}
		return res;
	}
	/**
	 * 删除选中菜单
	 * @returnet
	 */
	@GetMapping("delAllMenu")
	public ResultEntity delAllMenu(String IDS){
		ResultEntity res = new ResultEntity();
		try {
			String[] ids= IDS.split(",");
			for(String id : ids){
				sysMenuService.deleteMenu(id);
			}
			res.setMessage(DELETE_SUCCESS);
			res.setCode(SUCCESS);
		} catch(Exception e){
			e.printStackTrace();
			res.setMessage("删除失败："+e.getMessage());
			res.setMessage(DELETE_ERROR+":"+e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}
	
	/**
	 * 查询单个用户
	 * @return
	 */
	@GetMapping("findMenu")
	public ResultEntity findMenu(String ID){
		ResultEntity res = new ResultEntity();
		try {
			res.setData(sysMenuService.findMenu(ID));
			res.setMessage(GET_SUCCESS);
			res.setCode(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			res.setMessage(GET_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}
	
	/**
	 * 查询上级菜单
	 * @return
	 */
	@GetMapping("findTopList")
	public ResultEntity findTopList(){
		ResultEntity res = new ResultEntity();
		try {
			res.setData(MenuUtil.getAllDepartments(sysMenuService.findTopList()));
			res.setMessage(GET_SUCCESS);
			res.setCode(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			res.setMessage(GET_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}
	
	/**
	 * 查询上级菜单
	 * @return
	 */
	@GetMapping("findAll")
	public ResultEntity findAll(){
		ResultEntity res = new ResultEntity();
		try {
			res.setData(sysMenuService.findAll());
			res.setMessage(GET_SUCCESS);
			res.setCode(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			res.setMessage(GET_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}
}
